package controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import pojo.Room;
import service.RoomService;

import java.util.List;

@Api(value = "房间管理", description = "医院房间相关接口")
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/room")
public class RoomController {

    @Autowired
    private RoomService roomService;

    @ApiOperation(value = "查询所有房间", notes = "获取医院所有房间信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功返回房间列表", response = Room.class, responseContainer = "List")
    })
    @GetMapping("/findAll")
    public ResponseEntity<List<Room>> findAll() {
        return ResponseEntity.ok(roomService.findAll());
    }

    @ApiOperation(value = "根据ID查询房间", notes = "通过roomId获取单个房间信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功返回房间", response = Room.class),
            @ApiResponse(code = 404, message = "未找到对应房间")
    })
    @GetMapping("/findById/{roomId}")
    public ResponseEntity<Room> findById(@PathVariable Integer roomId) {
        Room room = roomService.findById(roomId);
        return room != null ? ResponseEntity.ok(room) : ResponseEntity.notFound().build();
    }

    @ApiOperation(value = "新增房间", notes = "创建一个新的房间")
    @ApiResponses({
            @ApiResponse(code = 201, message = "房间创建成功"),
            @ApiResponse(code = 400, message = "无效的房间数据")
    })
    @PostMapping("/addRoom")
    public ResponseEntity<Void> addRoom(@RequestBody Room room) {
        roomService.addRoom(room);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @ApiOperation(value = "更新房间信息", notes = "根据房间ID更新信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "房间信息更新成功"),
            @ApiResponse(code = 404, message = "未找到对应房间")
    })
    @PostMapping("/updateRoom")
    public ResponseEntity<Void> updateRoom(@RequestBody Room room) {
        int success = roomService.updateRoom(room);
        return success > 0 ?
                ResponseEntity.ok().build() :
                ResponseEntity.notFound().build();
    }

    @ApiOperation(value = "删除房间", notes = "根据房间ID删除记录")
    @ApiResponses({
            @ApiResponse(code = 200, message = "房间删除成功"),
            @ApiResponse(code = 404, message = "未找到对应房间")
    })
    @DeleteMapping("/deleteRoom/{roomId}")
    public ResponseEntity<Void> deleteRoom(@PathVariable Integer roomId) {
        int success = roomService.deleteRoom(roomId);
        return success > 0 ?
                ResponseEntity.ok().build() :
                ResponseEntity.notFound().build();
    }


     @GetMapping("/findByDepartId1")
    public List<Room> findByDepartId1(@RequestParam String departId1) {
        return roomService.findByDepartId1(Integer.parseInt(departId1));
    }
}